package com.ruoyi.callback.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.callback.service.CarService;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.modules.domain.VehicleInfo;

import com.ruoyi.modules.service.IVehicleInfoService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


/**
 * @date 2024/12/28 11:19
 * @desc 车辆回调 实现类
 **/
@Service
public class CarServiceImpl implements CarService {




    @Autowired
    private IVehicleInfoService vehicleInfoService;




//    {
//        "atime": "2024-12-27 20:04:12",
//            "auser": "admin_gj",
//            "bxts": "{\"1\":\"1\",\"2\":\"8\",\"1.text\":\"新增\",\"车辆营运类型\":\"1\",\"车辆运维状态\":\"2\",\"车辆颜色\":\"3\",\"车辆吨位\":\"4\",\"dbd\":\"1\",\"dbd.text\":\"是\",\"联系电话\":\"5\",\"车辆品牌\":\"6\",\"车辆类型\":\"7\",\"cargo_volume\":\"9\",\"special_license_plate\":\"10\",\"vehicle_star\":\"11\",\"vehicle_brand\":\"12\",\"vehicle_model\":\"13\",\"vehicle_type\":\"14\",\"dealer\":\"15\",\"production_date\":\"16\",\"annual_inspection_date\":\"17\",\"valid_until\":\"18\",\"registration_date\":\"19\",\"elimination_date\":\"20\",\"old_car_id\":\"21\",\"vehicle_color\":\"22\",\"vehicle_age\":\"23\",\"license_attachment\":\"2\",\"other_attachment\":\"25\",\"vehicle_photo_attachment\":\"26\",\"vehicle_registration_attachment\":\"27\",\"cargo_box_photo_attachment\":\"28\",\"vehicle_driving_license_attachme\":\"29\",\"exts\":\"30\",\"driver_id\":\"31\",\"own_name\":\"32\",\"contact\":\"33\",\"vin\":\"34\",\"team_id\":\"32\",\"plate_color\":\"35\",\"sim_card\":\"36\",\"tmn_no\":\"31\",\"tmn_key\":\"38\",\"key_type\":\"39\",\"enterprise_id\":\"10\",\"enterprise_name\":\"41\",\"type\":\"42\",\"channel_count\":\"43\",\"audio_code\":\"44\",\"cloud_channel\":\"45\",\"comm_channel\":\"49\",\"broadcast_channel\":\"70\",\"customized_channels\":\"72\",\"customized_names\":\"73\",\"old_tmn_id\":\"74\",\"province_code\":\"75\",\"province_name\":\"76\",\"city_code\":\"77\",\"city_name\":\"78\",\"area_code\":\"79\",\"area_name\":\"80\",\"town_name\":\"81\",\"town_code\":\"82\",\"所属企业\":\"84\",\"leixing\":\"1\",\"leixing.text\":\"货运\"}",
//            "cmpId": "7",
//            "des": "12",
//            "des2": "12",
//            "id": "69134",
//            "name": "21",
//            "plate": "21",
//            "plateColor": "2",
//            "state": 1,
//            "stts": "100||||",
//            "teamId": "4458",
//            "tmnId": "70310",
//            "tmnKey": "2122",
//            "utime": "2024-12-27 20:04:12",
//            "videos": "{\"t\":\"bb\",\"vs\":\"1::0:v,2::0:v,3::0:v,4::0:v\"}"
//    }

    /**
     * 车辆-->添加回调
     */
    @Override
    public void addCarCallback(String body) {
        JSONObject jsonObject = JSONObject.parseObject(body);
        String carId = jsonObject.getString("id") == null ? "" : jsonObject.getString("id");
        VehicleInfo vehicleInfo = new VehicleInfo();
        vehicleInfo.setOldCarId(carId);
        List<VehicleInfo> vehicleInfoList = vehicleInfoService.selectVehicleInfoList(vehicleInfo);
        if(vehicleInfoList.size()>0){
            System.out.println("车辆已经添加,请勿重新添加");
        }else{
            VehicleInfo  info = this.getVehicleInfo(body,jsonObject);

            int row = vehicleInfoService.insertVehicleInfo(info);
            if(row>0){
                System.out.println("车辆回调添加成功,车辆id: "+carId);
            }else{
                System.out.println("车辆回调添加失败,车辆id: "+carId);
            }
        }

    }

    /**
     * 车辆-->修改回调
     */
    @Override
    public void updateCarCallback(String body) {
        JSONObject jsonObject = JSONObject.parseObject(body);
        String carId = jsonObject.getString("id") == null ? "" : jsonObject.getString("id");
        VehicleInfo vehicleInfo = new VehicleInfo();
        vehicleInfo.setOldCarId(carId);
        List<VehicleInfo> vehicleInfoList = vehicleInfoService.selectVehicleInfoList(vehicleInfo);
        if(vehicleInfoList.size()>0){
            System.out.println("车辆已经修改");
            VehicleInfo  info = this.getVehicleInfo(body,jsonObject);
            info.setId(vehicleInfoList.get(0).getId());
            int row = vehicleInfoService.updateVehicleInfo(info);
            if(row>0){
                System.out.println("车辆回调修改成功,车辆id: "+carId);
            }else{
                System.out.println("车辆回调修改失败,车辆id: "+carId);
            }
        }else{
            System.out.println("没有改车辆信息,车辆id:"+carId);
        }
    }

    /**
     * 车辆-->删除回调
     */
    @Override
    public void delCarCallback(String body) {
        JSONObject jsonObject = JSONObject.parseObject(body);
        String carId = jsonObject.getString("id") == null ? "" : jsonObject.getString("id");
        VehicleInfo vehicleInfo = new VehicleInfo();
        vehicleInfo.setOldCarId(carId);
        List<VehicleInfo> vehicleInfoList = vehicleInfoService.selectVehicleInfoList(vehicleInfo);
        if(vehicleInfoList.size()>0){
            System.out.println("车辆已经删除,系统车辆id："+vehicleInfoList.get(0).getId());

            int row = vehicleInfoService.deleteVehicleInfoById(vehicleInfoList.get(0).getId());
            if(row>0){
                System.out.println("车辆回调删除成功,车辆id: "+carId);
            }else{
                System.out.println("车辆回调删除失败,车辆id: "+carId);
            }
        }else{
            System.out.println("没有改车辆信息,北斗车辆id:"+carId);
        }
    }


    private VehicleInfo  getVehicleInfo(String str,JSONObject jsonObject) {
        VehicleInfo vehicleInfo = new VehicleInfo();

        vehicleInfo.setTeamId(jsonObject.getString("teamId")==null?"":jsonObject.getString("teamId"));// 原始4g车队id
        String strData = jsonObject.getString("atime");
        Date s = DateUtils.parseDate(strData);
        vehicleInfo.setCreateTime(s);
        vehicleInfo.setExts(jsonObject.getString("exts")==null?"":jsonObject.getString("exts"));
        vehicleInfo.setOtherAttachment(str);//其他-附件
        vehicleInfo.setContact(jsonObject.getString("des")==null?"":jsonObject.getString("des"));
        vehicleInfo.setOldCarId(jsonObject.getString("id")==null?"":jsonObject.getString("id"));// 车辆id
        vehicleInfo.setName(jsonObject.getString("name")==null?"":jsonObject.getString("name"));//车辆名称
        vehicleInfo.setPlate(jsonObject.getString("plate")==null?"":jsonObject.getString("plate"));
        vehicleInfo.setPlateColor(jsonObject.getString("plateColor")==null?"":jsonObject.getString("plateColor"));
        vehicleInfo.setBxts(jsonObject.getString("bxts")==null?"":jsonObject.getString("bxts"));//其它扩展
        vehicleInfo.setOwnName(jsonObject.getString("owner")==null?"":jsonObject.getString("owner"));//车主
        vehicleInfo.setOwnerPhone(jsonObject.getString("ownerPhone")==null?"":jsonObject.getString("ownerPhone"));//车主电话
        vehicleInfo.setRegistrationDate(jsonObject.getString("onlineTime")==null?"":jsonObject.getString("onlineTime"));//首次上线时间
        vehicleInfo.setCarUsageStatus("0");//车辆使用状态：0空闲中 1.使用中2.维修中
        String bxts = jsonObject.getString("bxts") == null ? "" : jsonObject.getString("bxts");
        if(StringUtils.hasText(bxts)){
            JSONObject jsonBxtsObject = JSONObject.parseObject(bxts);
            String s1 = jsonBxtsObject.getString("car_load_bearing") == null ? "0" : jsonBxtsObject.getString("car_load_bearing");
            if(StringUtils.hasText(s1)){
                vehicleInfo.setCarLoadBearing(Long.parseLong(s1));//车辆吨位
            }else{
                vehicleInfo.setCarLoadBearing(0L);
            }
        }

        return vehicleInfo;


    }

}
